﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UdfGridReportSettingDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUdfGridReportSettingDetailDA UdfGridReportSettingDetailDA = DataAccess.CreateUdfGridReportSettingDetailDA();

        public UdfGridReportSettingDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public UdfGridReportSettingDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UdfGridReportSettingDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1870_002", this.pubParasInfo.UserLanguage);
                }
                List<UdfGridReportSettingDetailInfo> UdfGridReportSettingDetailInfos = new List<UdfGridReportSettingDetailInfo>();
                UdfGridReportSettingDetailInfo UdfGridReportSettingDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    UdfGridReportSettingDetailInfo = UdfGridReportSettingDetailDA.GetUdfGridReportSettingDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0], this.pubParasInfo);
                    UdfGridReportSettingDetailInfos.Add(UdfGridReportSettingDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (UdfGridReportSettingDetailDA.DeleteUdfGridReportSettingDetail(dataBase, tran, mainID, subId, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<UdfGridReportSettingDetailInfo>(dataBase, tran, UdfGridReportSettingDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return UdfGridReportSettingDetailDA.GetAllUdfGridReportSettingDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            UdfGridReportSettingDetailQueryEntity UdfGridReportSettingDetail = obj as UdfGridReportSettingDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.ReportID))
            {
                sqlWhere.Add(" REPORT_ID " + UdfGridReportSettingDetail.ReportID);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.LineID.ToString()))
            {
                sqlWhere.Add(" LINE_ID " + UdfGridReportSettingDetail.LineID.ToString());
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.CtrlID))
            {
                sqlWhere.Add(" CTRL_ID " + UdfGridReportSettingDetail.CtrlID);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.CtrlLabel))
            {
                sqlWhere.Add(" CTRL_LABEL " + UdfGridReportSettingDetail.CtrlLabel);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.CtrlType))
            {
                sqlWhere.Add(" CTRL_TYPE " + UdfGridReportSettingDetail.CtrlType);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.DefaultValue))
            {
                sqlWhere.Add(" DEFAULT_VALUE " + UdfGridReportSettingDetail.DefaultValue);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.ParamName))
            {
                sqlWhere.Add(" PARAM_NAME " + UdfGridReportSettingDetail.ParamName);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.IsMustInput))
            {
                sqlWhere.Add(" IS_MUST_INPUT " + UdfGridReportSettingDetail.IsMustInput);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.Udf1))
            {
                sqlWhere.Add(" UDF1 " + UdfGridReportSettingDetail.Udf1);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.Udf2))
            {
                sqlWhere.Add(" UDF2 " + UdfGridReportSettingDetail.Udf2);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.Udf3))
            {
                sqlWhere.Add(" UDF3 " + UdfGridReportSettingDetail.Udf3);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.Udf4))
            {
                sqlWhere.Add(" UDF4 " + UdfGridReportSettingDetail.Udf4);
            }
            if (!string.IsNullOrEmpty(UdfGridReportSettingDetail.Udf5))
            {
                sqlWhere.Add(" UDF5 " + UdfGridReportSettingDetail.Udf5);
            }
            return UdfGridReportSettingDetailDA.GetUdfGridReportSettingDetailByQueryList(sqlWhere, UdfGridReportSettingDetail, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return UdfGridReportSettingDetailDA.GetUdfGridReportSettingDetailByID(mainID, subID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<UdfGridReportSettingDetailInfo> UdfGridReportSettingDetail = UdfGridReportSettingDetailDA.GetUdfGridReportSettingDetailListByID(mainID, this.dataBaseID[0], this.pubParasInfo);
            List<object> objs = null;
            if ((UdfGridReportSettingDetail != null) && (UdfGridReportSettingDetail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < UdfGridReportSettingDetail.Count; i++)
                {
                    objs.Add(UdfGridReportSettingDetail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            UdfGridReportSettingDetailInfo UdfGridReportSettingDetailInfo = obj as UdfGridReportSettingDetailInfo;
            string LineID = string.Empty;
            try
            {
                if (UdfGridReportSettingDetailDA.CheckUdfGridReportSettingDetailIDUnique(UdfGridReportSettingDetailInfo.ReportID, UdfGridReportSettingDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1870_001", this.pubParasInfo.UserLanguage, new List<string> { UdfGridReportSettingDetailInfo.ReportID, UdfGridReportSettingDetailInfo.LineID.ToString() });
                }
                if (UdfGridReportSettingDetailInfo == null)
                {
                    return LineID;
                }
                UdfGridReportSettingDetailInfo.UpdatedBy = UdfGridReportSettingDetailInfo.CreatedBy;
                UdfGridReportSettingDetailInfo.CreatedDate = DateTime.Now;
                UdfGridReportSettingDetailInfo.UpdatedDate = DateTime.Now;
                if (UdfGridReportSettingDetailDA.InsertUdfGridReportSettingDetail(obj as UdfGridReportSettingDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UdfGridReportSettingDetailInfo>(null, UdfGridReportSettingDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                    LineID = UdfGridReportSettingDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                UdfGridReportSettingDetailInfo UdfGridReportSettingDetailInfo = obj as UdfGridReportSettingDetailInfo;
                UdfGridReportSettingDetailInfo oldUdfGridReportSettingDetailInfo = UdfGridReportSettingDetailDA.GetUdfGridReportSettingDetailByID(UdfGridReportSettingDetailInfo.ReportID, UdfGridReportSettingDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (UdfGridReportSettingDetailDA.UpdateUdfGridReportSettingDetail(UdfGridReportSettingDetailInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UdfGridReportSettingDetailInfo>(oldUdfGridReportSettingDetailInfo, UdfGridReportSettingDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "UDF_REPORT_SETTING_DETAIL", this.dataBaseID[0]);
                    LineID = UdfGridReportSettingDetailInfo.ReportID + " , " + UdfGridReportSettingDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

